<template>
  <div>
    <el-dialog :title="meta" :visible="showDialog" @close="closeForm">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model.trim="form.eid" />
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model.trim="form.short_name" />
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model.trim="form.intro" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" />
        </el-form-item>
        <el-form-item style="display: flex; justify-content: center">
          <el-button @click="closeForm">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getEnList, addEn, editEn } from '../../../api/enterprise'
export default {
  data() {
    var testEid = async(rule, value, callback) => {
      if (this.meta === '新增企业') {
        const res = await getEnList()
        if (res.data.items.find(v => v.eid === value)) {
          callback(new Error('企业编号重复'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var testName = async(rule, value, callback) => {
      if (this.meta === '新增企业') {
        const res = await getEnList()
        if (res.data.items.find(v => v.phone === value)) {
          callback(new Error('手机号重复'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    return {
      meta: '新增企业',
      showDialog: false,
      form: {},
      formLabelWidth: '120px',
      rules: {
        eid: [
          { required: true, message: '请输入企业编号', trigger: 'blur' },
          { validator: testEid, message: '企业编号重复', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入企业名称', trigger: 'blur' },
          { validator: testName, message: '企业名称重复', trigger: 'blur' }],
        short_name: [{ required: true, message: '请输入企业简称', trigger: 'blur' }],
        intro: [{ required: true, message: '请输入企业简介', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.meta === '新增企业') {
            await addEn(this.form)
            this.$message.success('新增企业成功')
          } else {
            await editEn(this.form)
            this.$message.success('编辑企业成功')
          }
          this.closeForm()
          this.$emit('getEnList')
        } else {
          this.message.success('校验不通过')
          return false
        }
      })
    },
    closeForm() {
      this.form = {}
      this.showDialog = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

  <style scoped lang="scss">
  ::v-deep {
    .el-dialog__header {
      background-color: #07b3fa;
      text-align: center;
    }
    .el-dialog__title,
    .el-dialog__close {
      color: #fff;
    }
    .el-dialog__close:hover {
      color: #fff;
    }
  }
  </style>

